package chen.javaee_experiment3.Service.Impl;

import chen.javaee_experiment3.Entity.VirusData;
import chen.javaee_experiment3.Service.SearchService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class SearchServiceImpl implements SearchService {

    @Override
    public List<VirusData> startWithKey(String key) {

        virusServiceImpl searchServiceImpl = new virusServiceImpl();
        Stream<VirusData> virusDataStream = searchServiceImpl.analysisCSV().parallelStream();

        if (key == null){
            return virusDataStream.sorted((virusData1, virusData2) -> -Integer.compare(virusData1.getLatestTotalCases(), virusData2.getLatestTotalCases()))
                    .collect(Collectors.toList());
        }

        List<VirusData> virusDataList = virusDataStream.filter(virusData -> virusData.getState().startsWith(key) || virusData.getCountry().startsWith(key))
                .sorted((virusData1, virusData2) -> -Integer.compare(virusData1.getLatestTotalCases(), virusData2.getLatestTotalCases()))
                .collect(Collectors.toList());
        return virusDataList;
    }
}
